package com.xiangyi.teachingsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiangyi.teachingsystem.entity.dto.PrivateResUserInfoDto;
import com.xiangyi.teachingsystem.entity.po.PrivateRes;
import com.xiangyi.teachingsystem.entity.po.PrivateResShare;
import com.xiangyi.teachingsystem.entity.po.User;
import com.xiangyi.teachingsystem.entity.vo.Result;
import com.xiangyi.teachingsystem.service.PrivateResService;
import com.xiangyi.teachingsystem.service.PrivateResShareService;
import com.xiangyi.teachingsystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class PrivateResShareController {
    @Autowired
    private PrivateResShareService privateResShareService;
    @Autowired
    private UserService userService;
    @Autowired
    private PrivateResService privateResService;

    @PostMapping("/addBatchShare")
    public Result addBatchShare(@RequestBody List<PrivateResShare> privateResShareList){
        try {
            HashMap<String, List<PrivateResShare>> errMap = new HashMap<>();
            for (PrivateResShare privateResShare : privateResShareList) {
                if (userService.getOne(new QueryWrapper<User>().eq("id",privateResShare.getUserId()))==null){
                    if (errMap.get("ExistUserId")==null){
                        ArrayList<PrivateResShare> resShares = new ArrayList<>();
                        resShares.add(privateResShare);
                        errMap.put("ExistUserId",resShares);
                    }else {
                        errMap.get("ExistUserId").add(privateResShare);
                    }
                    continue;
                }

                if (privateResService.getOne(new QueryWrapper<PrivateRes>().eq("id",privateResShare.getResId()))==null){
                    if (errMap.get("ExistResId")==null){
                        ArrayList<PrivateResShare> resShares = new ArrayList<>();
                        resShares.add(privateResShare);
                        errMap.put("ExistResId",resShares);
                    }else {
                        errMap.get("ExistResId").add(privateResShare);
                    }
                    continue;
                }
                privateResShareService.save(privateResShare);
            }
            if (!errMap.isEmpty())
                return Result.failure("部分资源添加共享用户失败").setData(errMap);
            return Result.success("资源添加共享用户全部成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @PostMapping("/addShare")
    public Result addShare(@RequestParam Integer resId,@RequestParam String phoneNum){
        PrivateResShare check = privateResShareService.getOne(new QueryWrapper<PrivateResShare>().eq("res_id", resId)
                .eq("phone_num", phoneNum));
        if (check!=null){
            return Result.failure("该用户已有此资源权限");
        }
        try {
            User userInfo = userService.getOne(new QueryWrapper<User>().eq("phone_num", phoneNum));
            if (userInfo==null)
                return Result.failure("请确认对方手机号");
            PrivateResShare privateResShare = new PrivateResShare();
            privateResShare.setResId(resId);
            privateResShare.setPhoneNum(phoneNum);
            privateResShare.setUserId(userInfo.getId());
            privateResShareService.save(privateResShare);
            return Result.failure("权限添加成功").setData(userInfo);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @PostMapping("delBatchShare")
    public Result delBatchShare(@RequestBody List<PrivateResShare> privateResShareList){
        try {
            for (PrivateResShare privateResShare : privateResShareList) {
                privateResShareService.remove(new QueryWrapper<PrivateResShare>()
                        .eq("user_id",privateResShare.getUserId())
                        .eq("res_id",privateResShare.getResId()));
            }
            return Result.success("批量移除分享用户成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @GetMapping("/getShearedResList")
    public Result getShearedResList(@RequestParam Integer userId){
        try {
            List<PrivateResShare> privateResShareList= privateResShareService.getShearedResList(userId);
            return Result.success("共享给我的资源列表查询成功").setData(privateResShareList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @GetMapping("/getSheareResList")
    public Result getSheareResList(@RequestParam Integer userId){
        try {
            List<PrivateResShare> privateResShareList= privateResShareService.getSheareResList(userId);
            return Result.success("我共享的资源列表查询成功").setData(privateResShareList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @GetMapping("/delBatchShareRes")
    public Result delBatchShareRes(@RequestParam List<Integer> idList){
        try {
            privateResShareService.removeByIds(idList);
            return Result.success("资源批量取消分享成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @GetMapping("/getShareUsersListByResId")
    public Result getShareUsersListByResId(@RequestParam Integer resId){
        try {
            List<PrivateResUserInfoDto> shareUsersListByResId = privateResShareService.getShareUsersListByResId(resId);
            return Result.success("查询成功").setData(shareUsersListByResId);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误");
        }
    }

    @PostMapping("/updateBatchShare")
    public Result updateBatchShare(@RequestBody Map<String, Object> map){
        Integer resId = (Integer) map.get("resId");
        List<Integer> userIds = (List<Integer>) map.get("userIds");
        privateResShareService.updateBatchShare(resId,userIds);
        return Result.success("添加成功");
    }


}
